/**
* Create by mzq
* Description: MyHeader
* Date: 2025/2/13 14:59
* Update: 2025/2/13 14:59
*/


<template>
  <header class="header" @click="handlePageClick">
    <!-- 功能导航菜单 -->
    <nav class="nav-menu">
      <div 
        class="nav-item" 
        v-for="(item, index) in navItems" 
        :key="index"
        v-if="item.show"
        @click="handleNavClick(item)">
        <image :src="item.src" :alt="item.alt" :class="{ 'disabled': item.disabled }" />
      </div>
    </nav>
    <!-- 用户信息区域 -->
    <div class="user-info">
      <div class="userIcon">
        <image src="../../../image/index/user.png"></image>
      </div>
      <div class="username" @click.stop="toggleDropdown">
        <span>{{ username }}</span>
        <span class="triangle">▼</span>
      </div>
      <div class="line"></div>
      <div class="home-icon" @click="handleHomeClick">
        <image src="../../../image/monitor/home.png"></image>
      </div>

      <!-- 下拉菜单 -->
      <div v-if="showDropdown" class="dropdownMenu" @click.stop>
        <div class="userTitle">
          <div class="leftUserTitle">
            <image src="../../../image/icon_eilogo.png"></image>
          </div>
          <div class="rightUserTitle">
            <div class="firstUserTitle">{{ username }}</div>
            <div>ID:{{ id }}</div>
          </div>
        </div>

        <div class="list">
          <div
            @click="handleMenuItemClick(item.title)"
            class="listContent"
            v-for="item in list"
            v-if="item.role ? username === item.role : true">
            <div>
              <image
                style="width: 20px; height: 20px"
                :src="item.imageUrl"></image>
            </div>
            <div>{{ item.title }}</div>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script>
import { writeBLEState } from '@/public/BluetoothFun'

export default {
  name: 'MyHeader',
  props: {
    username: {
      type: String,
      required: true
    },
    navItems: {
      type: Array,
      required: true
    },
    isMonitoring: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showDropdown: false,
      list: [
        {
          id: 1,
          title: '账号管理',
          imageUrl: require('@/static/userMenu/accountManagement.png'),
          role: 'sysadmin',
        },
        {
          id: 2,
          title: '资料中心',
          imageUrl: require('@/static/userMenu/DataCenter.png')
        },
        {
          id: 3,
          title: '修改密码',
          imageUrl: require('@/static/userMenu/ChangePassword.png'),
        },
        {
          id: 4,
          title: '退出登录',
          imageUrl: require('@/static/userMenu/logOut.png'),
        },
      ],
    }
  },
  methods: {
    handleNavClick(item) {
      this.$emit('navClick', item)
    },
    handleHomeClick() {
      if(this.isMonitoring) {
        uni.showToast({
          title: '监测过程中无法返回首页',
          icon: 'none'
        })
        return
      }
      this.$emit('homeClick')
      
      writeBLEState(99)
      uni.reLaunch({
        url: '/pages/userMenu/userMenu'
      })
    },
    toggleDropdown() {
      if(this.isMonitoring) {
        uni.showToast({
          title: '监测过程中无法操作',
          icon: 'none'
        })
        return
      }
      this.showDropdown = !this.showDropdown
    },
    handleMenuItemClick(option) {
      this.$emit('menuItemClick', option)
      this.showDropdown = false
    },
    handlePageClick() {
      if (this.showDropdown) {
        this.showDropdown = false
      }
    }
  },
  mounted() {
    // 移除全局点击事件监听
  },
  beforeDestroy() {
    // 移除全局点击事件清理
  }
}
</script>

<style scoped lang="scss">
.header {
  width: 100%;
  height: 80px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #fff;

  .nav-menu {
    display: flex;
    margin-left: 320px;

    .nav-item {
      display: flex;
      align-items: center;
      cursor: pointer;

      image {
        width: 80px;
        height: 24px;
        object-fit: contain;

        &.disabled {
          opacity: 0.5;
          cursor: not-allowed;
        }
      }

      &:hover {
        opacity: 0.8;
      }
    }
  }

  .user-info {
    padding-top: 10px;
    padding-right: 40px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: right;
    position: relative;

    .userIcon {
      margin-right: 10px;
    }

    .username {
      cursor: pointer;
      color: #6DF7BE;
      &:hover {
        opacity: 0.8;
      }
    }

    .line {
      width: 1px;
      height: 20px;
      background-color: #6DF7BE;
      margin: 0 15px;
    }

    .home-icon {
      cursor: pointer;
      &:hover {
        opacity: 0.8;
      }
    }

    image {
      width: 20px;
      height: 20px;
    }

    div {
      display: flex;
      align-items: center;
    }

    span {
      font-size: 14px;
      color: #fff;

      &.triangle {
        margin-left: 4px;
        font-size: 12px;
      }
    }

    .dropdownMenu {
      border-radius: 5px;
      min-width: 200px;
      position: absolute;
      top: 45px;
      right: 60px;
      background-color: #fff;
      border: 1px solid rgba(0, 0, 0, 0.1);
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      z-index: 1000;
      padding: 10px 0;
      display: flex;
      flex-direction: column;

      .userTitle {
        padding: 15px;
        border-bottom: 1px solid #eee;

        .leftUserTitle {
          image {
            width: 40px;
            height: 40px;
          }
        }

        .rightUserTitle {
          margin-left: 15px;
          display: flex;
          flex-direction: column;
          justify-content: flex-start;
          color: #333;

          .firstUserTitle {
            font-size: 16px;
            font-weight: 500;
            margin-bottom: 4px;
            text-align: left;
            width: 100%;
            text-align: left;
          }

          div:last-child {
            font-size: 14px;
            color: #666;
          }
        }
      }

      .list {
        width: 100%;
        margin-top: 10px;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        text-align: left;

        .listContent {
          width: 100%;
          height: 40px;
          padding: 0 20px;
          display: flex;
          align-items: center;
          color: #2c2c2c;
          cursor: pointer;
          text-align: left;
          padding-left: 60px;

          &:hover {
            background-color: rgba(109, 247, 190, 0.1);
          }

          image {
            width: 20px;
            height: 20px;
            margin-right: 10px;
          }

          div:last-child {
            font-size: 14px;
            margin-left: 5px;
          }
        }
      }
    }
  }
}
</style>